<!--委托公司-->
<template>
	<a-card :bordered="false">
		<!-- <a-space>
			<a-input-search v-model:value="searchFormState.companyName" placeholder="请输入公司名称" allowClear
				@search="onSearch" />
		</a-space> -->
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="公司名称" name="debtorName">
						<a-input
							v-model:value.trim="searchFormState.companyName"
							placeholder="请输入公司名称"
							allowClear
							@pressEnter="onSearch"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" @click="onSearch">查询</a-button>
				</a-col>
			</a-row>
		</a-form>
	</a-card>
	<a-card :bordered="false" class="mt-2">
		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<template #operator>
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()">
						<template #icon>
							<plus-outlined />
						</template>
						新增
					</a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="formRef.onOpen(record)">编辑</a>
						<a-divider type="vertical" />
						<a-popconfirm title="确定要删除此公司吗？" @confirm="deleteModule(record)">
							<a-button type="link" danger size="small">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh(true)" />
</template>

<script setup name="Company">
	import companyApi from '@/api/tjzy/company/companyApi'
	import Form from '@/views/tjzy/company/form.vue'
	import moduleApi from '@/api/sys/resource/moduleApi'

	const searchFormState = ref({
		companyName: ''
	})
	const tableRef = ref()
	const formRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: false, striped: false }
	const columns = [
		{
			title: '公司编码',
			dataIndex: 'companyCode'
		},
		{
			title: '公司名称',
			dataIndex: 'companyName'
		},
		{
			title: '税号/统一社会信用代码',
			dataIndex: 'unifiedSocialCreditCode'
		},
		{
			title: '联系人',
			dataIndex: 'concatName',
			ellipsis: true
		},
		{
			title: '联系人电话',
			dataIndex: 'concatPhone'
		},
		{
			title: '银行卡号',
			dataIndex: 'bankAccount',
			ellipsis: true
		},
		{
			title: '开户行',
			dataIndex: 'openingBank'
		},
		{
			title: '操作',
			dataIndex: 'action',
			width: '200px',
			scopedSlots: { customRender: 'action' }
		}
	]
	let selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		alert: {
			show: false,
			clear: () => {
				selectedRowKeys = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const loadData = (parameter) => {
		return companyApi.companyPage(Object.assign(parameter, searchFormState.value)).then((res) => {
			return res
		})
	}
	// 查询
	const onSearch = () => {
		tableRef.value.refresh(true)
	}
	// 删除
	const deleteModule = (record) => {
		let params = { companyIdList: [record.id] }
		companyApi.delCompany(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
</script>
